<template>
  <br /><br />
  <div>
    <el-button type="primary" @click="clickadd">确认新增</el-button
    >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <el-input
      fixed="right"
      v-model="list.classifyName"
      placeholder="请输入玩具名"
      style="width: 200px"
    />
    <!-- <el-button type="primary" @click="getlist()">查询</el-button> -->
    <el-button  @click="getlist()" :icon="Search" circle />
    <br /><br />
  </div>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="classifyName" label="分类名称" width="300" />
      <el-table-column prop="createTime" label="创建时间" width="300">
        <template #default="scope">
          {{ scope.row.createTime.substring(0, 10) }}
        </template>
      </el-table-column>
      <el-table-column prop="classifyDesc" label="备注" width="300" />
      <el-table-column  label="操作列" min-width="300">
        <template #default="{row}">
          <el-button link type="primary" size="small" @click="handleClick(row)">
            编辑
          </el-button>
          <el-button link type="primary" size="small" @click="clickdeleted(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" title="新增分类" width="500">
    <el-form :model="form">
      <el-form-item label="分类名称" :label-width="80">
        <el-input v-model="form.classifyName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="创建时间" :label-width="80">
        <el-input v-model="form.createTime" autocomplete="off" type="date"/>
      </el-form-item>
      <el-form-item label="备注" :label-width="80">
        <el-input v-model="form.classifyDesc" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogadd()">
          确认新增
        </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog v-model="dialogFormVisibles" title="修改分类" width="500">
    <el-form :model="forms">
      <el-form-item label="分类名称" :label-width="80">
        <el-input v-model="forms.classifyName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="创建时间" :label-width="80">
        <el-input v-model="forms.createTime" autocomplete="off" type="date"/>
      </el-form-item>
      <el-form-item label="备注" :label-width="80">
        <el-input v-model="forms.classifyDesc" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisibles = false">取消</el-button>
        <el-button type="primary" @click="dialogupdate()">
          确认修改
        </el-button>
      </div>
    </template>
  </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted ,reactive} from 'vue';
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

import {
  Check,
  Delete,
  Edit,
  Message,
  Search,
  Star,
} from '@element-plus/icons-vue'
const router = useRouter();





const dialogFormVisible = ref(false);
const dialogFormVisibles=ref(false);
const tableData = ref([]);
const list = ref({
  classifyName: ''
});
const form =reactive ({
  classifyName:'',
  classifyDesc:'',
  createTime:''
})
const forms =reactive ({
  id:'',
  classifyName:'',
  classifyDesc:'',
  createTime:''
})
//生命周期函数
onMounted(() => {
  getlist();
});

//点击编辑按钮
const handleClick = (row: any) => {
  dialogFormVisibles.value = true;
  console.log(row);
  forms.id=row.id;
  forms.classifyName=row.classifyName;
  forms.createTime=row.createTime.substring(0, 10);
  forms.classifyDesc=row.classifyDesc;

};

//获取列表
const getlist = () => {
  axios({
    url: 'https://localhost:44392/api/app/classify-manage-services/classify-manage',
    method: 'get',
    params: list.value
  }).then((res) => {
    console.log(res);
    tableData.value = res.data.data;
  });
};

//删除
const clickdeleted = (id:any) => {
  axios.delete("https://localhost:44392/DeleteClassifyManage?Id="+id).then(res=>{
    if(res.data.code==200)
  {
    ElMessage({
      message: '删除成功',
      type: 'success',
    })
    getlist();
  }else
  {
    ElMessage.error("删除失败")
  }
  })
}

//新增按钮
const clickadd = () => {
  dialogFormVisible.value = true;
};

//确认新增按钮
const dialogadd=()=>{
  axios({
    url:"https://localhost:44392/PostClassifyManage",
    method: 'post',
    data: form
  }).then(res=>{
    if(res.data.code==200)
  {
    ElMessage({
      message: '新增成功',
      type: 'success'
    })
    dialogFormVisible.value = false;
    getlist()
  }
  })
}

//确认修改按钮
const dialogupdate=()=>{
  axios({
    url:"https://localhost:44392/PutClassifyManage",
    method: 'put',
    data: forms
  }).then(res=>{
    if(res.data.code==200)
  {
    ElMessage({
      message: '修改成功',
      type: 'success'
    })
    dialogFormVisibles.value = false;
    getlist();
  }else
  {
    ElMessage({
      message: '修改失败',
      type: 'error'
    })
    
  }
  })
}
</script>

